package Demo5;

import java.util.Arrays;
import java.util.Comparator;

class Student /*implements Comparable<Student>*/{
    public String name;
    public int age;
    public int score;
    public Student(String name,int age,int score){
        this.name=name;
        this.age=age;
        this.score=score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' + ", age=" + age +
                ", score=" + score + '}';
    }
    //Comparable接口默认有个compareTo方法，现在重写
    /*public int compareTo(Student o){
        /*根据年龄比较
        if (this.age>o.age){
            return 1;
        } else if (this.age < o.age) {
            return -1;
        }else{
            return 0;
        }
        //比较字符串
        if (this.name.compareTo( o.name)>0){//调用String类中默认方法compareTo,比较第一个字母的ASCII码
            return 1;
        } else if (this.name.compareTo(o.name)<0){
            return -1;
        }else{
            return 0;
        }

    }*/
}
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age-o2.age;
    }
}
class ScoreComparator implements Comparator<Student>{
    public int compare(Student o1,Student o2){
        return o1.score-o2.score;
    }
}
class NameComparator implements Comparator<Student>{
    public int compare(Student o1,Student o2){
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    public static void main(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("ahangsan",19,20);
        students[1]=new Student("cisi",45,99);
        students[2]=new Student("baowu",33,21);
        AgeComparator ageComparator=new AgeComparator();
        ScoreComparator scoreComparator=new ScoreComparator();
        NameComparator nameComparator=new NameComparator();
        Arrays.sort(students,ageComparator);
        System.out.println(Arrays.toString(students));
    }
}
